﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>预约单管理</title>
    <#include "/common/link.ftl">
</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="appointment"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>预约单管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 20px 0px 0px 10px">
                    <form class="form-inline" id="searchForm" action="/appointment/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="${qo.currentPage}">
                        <div class="form-group">
                            <label>预约单流水号</label>
                            <input type="text" class="form-control" placeholder="请输入预约单流水号" name="ano"
                                   value="${qo.ano}">
                        </div>
                        <div class="form-group">
                            <label>预约业务大类</label>
                            <select class="form-control" id="categoryId" name="categoryId">
                                <option value="">请选择业务大类</option>
                                <#list category as tmp>
                                    <option value="${tmp.id}">${tmp.title}</option>
                                </#list>
                            </select>
                            <script>
                                $("#categoryId").val(${qo.categoryId})
                            </script>
                        </div>
                        <div class="form-group">
                            <label>预约单状态</label>
                            <select class="form-control" id="status" name="status">
                                <option value="">全部</option>
                                <#list status as tmp>
                                    <option value="${tmp.getStatus()}">${tmp.getName()}</option>
                                </#list>
                            </select>
                            <script>
                                $("#status").val(${qo.status})
                            </script>
                        </div>
                        <div class="form-group">
                            <label>门店查询</label>
                            <select class="form-control" id="businessId" name="businessId">
                                <option value="">请选择门店</option>
                                <#list businesses as tmp>
                                    <option value="${tmp.id}">${tmp.name}</option>
                                </#list>
                            </select>
                            <script>
                                $("#businessId").val(${qo.businessId})
                            </script>
                        </div>
                        <div class="form-group">
                            <label>客户名称</label>
                            <input type="text" class="form-control" placeholder="请输入客户名称" value="${qo.contactName}"
                                   name="contactName">
                        </div>

                        <div class="form-group">
                            <label>客户手机号</label>
                            <input type="text" class="form-control" placeholder="请输入客户手机号" value="${qo.contactTel}"
                                   name="contactTel">
                        </div>

                        <br/>
                        <br/>

                        <div class="form-group">
                            <label>预约时间查询：</label>
                            <input placeholder="请输入开始时间" id="beginDate" type="text" class="form-control "
                                   name="beginDate" value="${(qo.beginDate?string("yyyy-MM-dd HH:mm"))!}"/> -
                            <input placeholder="请输入结束时间" id="endDate" type="text" class="form-control " name="endDate"
                                   value="${(qo.endDate?string("yyyy-MM-dd HH:mm"))!}"/>
                        </div>

                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>
                            查询
                        </button>

                        <a href="javascript:;" class="btn btn-success btn-input addbtn">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                    </form>

                </div>
                <div class="box-body table-responsive ">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>流水号</th>
                            <th>业务大类</th>
                            <th>预约说明</th>
                            <th>预约时间</th>
                            <th>客户名称</th>
                            <th>联系方式</th>
                            <th>预约门店</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list result.list as tmp>
                            <tr>
                                <td>${tmp_index+1}</td>
                                <td>${tmp.ano}</td>
                                <td>${tmp.category.title}</td>
                                <td>${tmp.info}</td>
                                <td>${(tmp.appointmentTime?string("yyyy-MM-dd HH:mm"))!}</td>
                                <td>${tmp.contactName}</td>
                                <td>${tmp.contactTel}</td>
                                <td>${tmp.business.name}</td>
                                <td>${tmp.stautsName}</td>
                                <td>
                                    <a href="javascript:;" data-json='${tmp.json}'
                                       class="btn btn-info btn-xs btn-input btnedit">
                                        <span class="glyphicon glyphicon-pencil"></span> 编辑
                                    </a>
                                    <a class="btn btn-xs btn-primary btn-status"
                                       onclick="confirmAppointment(${tmp.id},${tmp.status})">
                                        <span class="glyphicon glyphicon-phone-alt"></span> 确认预约</a>
                                    <a class="btn btn-xs btn-danger btn-status"
                                       onclick="cancel(${tmp.id},${tmp.status})">
                                        <span class="glyphicon glyphicon-remove"
                                        ></span> 取消预约</a>
                                    <a href="javascript:;" class="btn btn-success btn-xs btn-consume"
                                       onclick="checkIn(${tmp.id},${tmp.status})">
                                        <span class="glyphicon glyphicon-shopping-cart"></span> 确认到店
                                    </a>
                                </td>
                            </tr>
                        </#list>
                        </tbody>
                    </table>
                    <!--分页-->
                    <#include "/common/page.ftl">
                </div>

            </div>
        </section>
    </div>
    <#include "/common/footer.ftl">
</div>


<#-- 文件上传模态框 -->
<!--模态框-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form id="editForm" action="/appointment/saveOrUpdate" method="post">
                <div class="modal-body">
                    <input type="hidden" name="id" id="id">
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">预约门店：</label>
                        <div class="col-sm-7">
                            <select class="form-control" name="business.id" id="businessFormId">
                                <option value="">请选择预约门店</option>
                                <#list businesses as tmp>
                                    <option value="${tmp.id}">${tmp.name}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">预约时间：</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control " id="appointmentTimeForm" name="appointmentTime"
                                   placeholder="请输入预约时间"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">业务大类：</label>
                        <div class="col-sm-7">
                            <select class="form-control" name="category.id" id="categoryFormId">
                                <option value="">请选择业务大类</option>
                                <#list category as tmp>
                                    <option value="${tmp.id}">${tmp.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">联系人：</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" name="contactName" id="contactName"
                                   placeholder="请输入联系人">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">联系电话：</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" name="contactTel" id="contactTel"
                                   placeholder="请输入联系电话">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">预约说明：</label>
                        <div class="col-sm-7">
                            <textarea type="text" class="form-control" name="info" id="info"
                                      placeholder="请输入预约说明"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-submit btnSubmit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $('#beginDate,#endDate,#appointmentTimeForm').datetimepicker({
        format: 'yyyy-mm-dd hh:ss', //格式
        language: 'zh-CN', //中文
        autoclose: true,//选择后自动关闭
        //showMeridian:true, //是否显示上下午
        minView: 0,//精确到哪位
    });
</script>
</body>
<script>
    $(".addbtn").click(function () {
        $("#editModal").modal("show")
    })

    $(".btnSubmit").click(function () {
        $("#editForm").submit();
    })

    $(".btnedit").click(function () {
        var json = $(this).data("json");
        $("#id").val(json.id);
        $("#businessFormId").val(json.business);
        $("#categoryFormId").val(json.category);
        $("#appointmentTimeForm").val(formatDateTime(json.appointmentTime));

        $("#info").val(json.info);
        $("#contactName").val(json.contactName);
        $("#contactTel").val(json.contactTel);
        $("#editModal").modal("show")
        console.log(json)
    })

    function formatDateTime(inputTime) {
        var date = new Date(inputTime);  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    };


    function modifyStatus(id, status, msg) {
        Swal.fire({
            title: '提示',
            text: msg,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '是!',
            cancelButtonText: '否!'
        }).then((result) => {
            if (result.value) {
                //点击确认按钮后做的事情
                $.post("/appointment/statusChange", {id: id, status: status}, function (data) {
                    if (data.success) {
                        Swal.fire({
                            title: '操作成功',
                            text: "预约单已更改",
                            icon: 'success',
                            timer: 2000,
                        }).then((result) => {
                            window.location.href = "/appointment/list";
                        })
                    }
                })
            }
        })
    }

    function modifyStatus2(id, status, msg) {
        Swal.fire({
            title: '提示',
            text: msg,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '是!',
            cancelButtonText: '否!'
        }).then((result) => {
            if (result.value) {
                //点击确认按钮后做的事情
                $.post("/appointment/statusChange", {id: id, status: status}, function (data) {
                    if (data.success) {
                        Swal.fire({
                            title: '操作成功',
                            text: "预约单已更改",
                            icon: 'success',
                            timer: 2000,
                        }).then((result) => {
                            window.location.href = "/appointment/list";
                        })
                    }
                })
            } else {
                $.post("/appointment/statusChange", {id: id, status: 3}, function (data) {
                    if (data.success) {
                        Swal.fire({
                            title: '操作成功',
                            text: "预约单已更改",
                            icon: 'success',
                            timer: 2000,
                        }).then((result) => {
                            window.location.href = "/appointment/list";
                        })
                    }
                })
            }
        })
    }

    function confirmAppointment(id, status) {
        //判断可行性
        // if (status != 0) {
        //     return;
        // }
        modifyStatus(id, 1, "是否已确定预约信息无误");
    }

    function checkIn(id, status) {
        // if (status != 1) {
        //     return;
        // }
        Swal.fire({
            title: '温馨提示',
            text: "是否已确定客户到店",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定!',
            cancelButtonText: '取消!'
        }).then((result) => {
            if (result.value) {
                //客户已到店
                modifyStatus2(id, 2, "客户是否已消费");
            } else {
                //客户未到店 无操作
            }
        })
    }

    function cancel(id, status) {
        // if (status != 0) {
        //     return;
        // }
        modifyStatus(id, 4, "是否已确定取消本次预约");
    }
</script>
</html>
